
$(document).ready( function() {

  function getGroupSummary( group, totals ) {
  
    var results = [];
  
    for(var i = 0; i < aria_test_results.results.length; i++ ) {
  
      if(aria_test_results.results[i].group == group ) {
        results.push( aria_test_results.results[i] );
      } // endif
      
    }  // endfor  

    var summary = {};
      summary.num = 0;
    
      summary.win7 = {};
      summary.win7.fail     = 0;
      summary.win7.pass     = 0;
      summary.win7.question = 0;
      summary.win7.untested = 0;

      summary.osx106 = {};
      summary.osx106.fail     = 0;
      summary.osx106.pass     = 0;
      summary.osx106.question = 0;
      summary.osx106.untested = 0;

      summary.win7uia = {};
      summary.win7uia.fail     = 0;
      summary.win7uia.pass     = 0;
      summary.win7uia.question = 0;
      summary.win7uia.untested = 0;

      summary.unix = {};
      summary.unix.fail     = 0;
      summary.unix.pass     = 0;
      summary.unix.question = 0;
      summary.unix.untested = 0;

      summary.sr = {};
      summary.sr.fail     = 0;
      summary.sr.pass     = 0;
      summary.sr.question = 0;
      summary.sr.untested = 0;

    for(var i = 0; i < results.length; i++ ) {
      
      var win7 = results[i].results.win7;
      if( win7 ) {
        for(var j=0; j < win7.length; j++) {
        
          switch( win7[j].result) {
            case 'f':
              summary.win7.fail++;
              totals.win7.fail++;
              break;

            case 'p':
              summary.win7.pass++;
              totals.win7.pass++;
              break;

            case '?':
              summary.win7.question++;
              totals.win7.question++;
              break;

            default:
              summary.win7.untested++;
              totals.win7.untested++;
              break;

          } // endswitch
          
        } // endfor
      } // endif


      var osx106 = results[i].results.osx106;
      if( osx106 ) {
        for(var j=0; j < osx106.length; j++) {
        
          switch( osx106[j].result) {
            case 'f':
              summary.osx106.fail++;
              totals.osx106.fail++;
              break;

            case 'p':
              summary.osx106.pass++;
              totals.osx106.pass++;
              break;

            case '?':
              summary.osx106.question++;
              totals.osx106.question++;
              break;

            default:
              summary.osx106.untested++;
              totals.osx106.untested++;
              break;

          } // endswitch
          
        } // endfor
      } // endif

      var unix = results[i].results.unix;
      if( unix ) {
        for(var j=0; j < unix.length; j++) {
        
          switch( unix[j].result) {
            case 'f':
              summary.unix.fail++;
              totals.unix.fail++;
              break;

            case 'p':
              summary.unix.pass++;
              totals.unix.pass++;
              break;

            case '?':
              summary.unix.question++;
              totals.unix.question++;
              break;

            default:
              summary.unix.untested++;
              totals.unix.untested++;
              break;

          } // endswitch
          
        } // endfor
      } // endif


      var win7uia = results[i].results.win7uia;
      if( win7uia ) {
        for(var j=0; j < win7uia.length; j++) {
        
          switch( win7uia[j].result) {
            case 'f':
              summary.win7uia.fail++;
              totals.win7uia.fail++;
              break;

            case 'p':
              summary.win7uia.pass++;
              totals.win7uia.pass++;
              break;

            case '?':
              summary.win7uia.question++;
              totals.win7uia.question++;
              break;

            default:
              summary.win7uia.untested++;
              totals.win7uia.untested++;
              break;

          } // endswitch
          
        } // endfor
      } // endif
      
     var sr = results[i].results.sr;
      if( sr ) {
        for(var j=0; j < sr.length; j++) {
        
          switch( sr[j].result) {
            case 'f':
              summary.sr.fail++;
              totals.sr.fail++;
              break;

            case 'p':
              summary.sr.pass++;
              totals.sr.pass++;
              break;

            case '?':
              summary.sr.question++;
              totals.sr.question++;
              break;

            default:
              summary.sr.untested++;
              totals.sr.untested++;
              break;

          } // endswitch
          
        } // endfor
      } // endif
      
      summary.num++;
      totals.num++;
    } // end for 
    
    return summary;
     
  }  // end getGroupTest

    var totals = {};
      totals.num = 0;
    
      totals.win7 = {};
      totals.win7.fail     = 0;
      totals.win7.pass     = 0;
      totals.win7.question = 0;
      totals.win7.untested = 0;

      totals.osx106 = {};
      totals.osx106.fail     = 0;
      totals.osx106.pass     = 0;
      totals.osx106.question = 0;
      totals.osx106.untested = 0;

      totals.win7uia = {};
      totals.win7uia.fail     = 0;
      totals.win7uia.pass     = 0;
      totals.win7uia.question = 0;
      totals.win7uia.untested = 0;

      totals.unix = {};
      totals.unix.fail     = 0;
      totals.unix.pass     = 0;
      totals.unix.question = 0;
      totals.unix.untested = 0;

      totals.sr = {};
      totals.sr.fail     = 0;
      totals.sr.pass     = 0;
      totals.sr.question = 0;
      totals.sr.untested = 0;


  var html = "";

  html += "<table class='summary' summary='Summary of group test results'>";
  html += "  <caption>Summary of Group Test Results</caption>";  
  html += "  <thead>";
  html += "    <tr>";
  html += "      <th id='gid'   rowspan='2'>Group<br/>Num</th>";
  html += "      <th id='gdesc' rowspan='2'>Description</th>";
  html += "      <th id='gnum'  rowspan='2'>Number<br/>Tests</th>";
  html += "      <th id='gmsaa' colspan='4' class='col1'>Windows 7 <abbr title='Microsoft Active Accessibility'>MSAA</abbr> &amp; <abbr title='IAccessible2'>IA2</abbr></th>";
  html += "      <th id='gosx'  colspan='4' class='col2'>OS X 10.6</th>";
  html += "      <th id='gunix' colspan='4' class='col3'>Unix/Linux</th>";
  html += "      <th id='guia'  colspan='4' class='col4'>Windows 7 UI Automation</th>";
  html += "      <th id='gsr'   colspan='4' class='col5'>Screen Reader</th>";
  html += "    </tr>";
  html += "    <tr>";
  html += "      <th id='pass' class='col11'>Pass</th>";
  html += "      <th id='fail' class='col12'>Fail</th>";
  html += "      <th id='question' class='col13'>Question</th>";
  html += "      <th id='untested' class='col14'>Untested</th>";
  html += "      <th class='col21'>Pass</th>";
  html += "      <th class='col22'>Fail</th>";
  html += "      <th class='col23'>Question</th>";
  html += "      <th class='col24'>Untested</th>";
  html += "      <th class='col31'>Pass</th>";
  html += "      <th class='col32'>Fail</th>";
  html += "      <th class='col33'>Question</th>";
  html += "      <th class='col34'>Untested</th>";
  html += "      <th class='col41'>Pass</th>";
  html += "      <th class='col42'>Fail</th>";
  html += "      <th class='col43'>Question</th>";
  html += "      <th class='col44'>Untested</th>";
  html += "      <th class='col51'>Pass</th>";
  html += "      <th class='col52'>Fail</th>";
  html += "      <th class='col53'>Question</th>";
  html += "      <th class='col54'>Untested</th>";
  html += "    </tr>";
  html += "  </thead>";

  html += "  <tbody>";
  for(var i = 0; i < aria_test_results.groups.length; i++ ) {
    var summary = getGroupSummary( aria_test_results.groups[i].num, totals );
    var row_id = 'sr' + i;
    html += "    <tr>";
    html += "      <th id='" + row_id + "' headers='gid'>";
    html += "        " + aria_test_results.groups[i].num; 
    html += "      </th>"; 
    html += "      <td headers='" + row_id + " gdesc' class='desc'>"; 
    html += "         <a href='" + aria_test_results.groups[i].url + "'>" + aria_test_results.groups[i].title + "</a>";
    html += "      </td>"; 
    html += "      <td headers='" + row_id + " gnum'>"; 
    html += "        " + summary.num;
    html += "      </td>" ;
    html += "      <td headers='" + row_id + " pass gwin7'        class='col11'>" + summary.win7.pass        + "</td>";
    html += "      <td headers='" + row_id + " fail gwin7'        class='col12'>" + summary.win7.fail        + "</td>";
    html += "      <td headers='" + row_id + " question gwin7'    class='col13'>" + summary.win7.question    + "</td>";
    html += "      <td headers='" + row_id + " untested gwin7'    class='col14'>" + summary.win7.untested    + "</td>";
    html += "      <td headers='" + row_id + " pass gosx106'      class='col21'>" + summary.osx106.pass      + "</td>";
    html += "      <td headers='" + row_id + " fail gosx106'      class='col22'>" + summary.osx106.fail      + "</td>";
    html += "      <td headers='" + row_id + " question gosx106'  class='col23'>" + summary.osx106.question  + "</td>";
    html += "      <td headers='" + row_id + " fail gosx106'      class='col24'>" + summary.osx106.untested  + "</td>";
    html += "      <td headers='" + row_id + " pass gunix'        class='col31'>" + summary.unix.pass        + "</td>";
    html += "      <td headers='" + row_id + " pass gunix'        class='col32'>" + summary.unix.fail        + "</td>";
    html += "      <td headers='" + row_id + " pass gunix'        class='col33'>" + summary.unix.question    + "</td>";
    html += "      <td headers='" + row_id + " pass gunix'        class='col34'>" + summary.unix.untested    + "</td>";
    html += "      <td headers='" + row_id + " pass gwin7uia'     class='col41'>" + summary.win7uia.pass     + "</td>";
    html += "      <td headers='" + row_id + " fail gwin7uia'     class='col42'>" + summary.win7uia.fail     + "</td>";
    html += "      <td headers='" + row_id + " question gwin7uia' class='col43'>" + summary.win7uia.question + "</td>";
    html += "      <td headers='" + row_id + " untested gwin7uia' class='col44'>" + summary.win7uia.untested + "</td>";
    html += "      <td headers='" + row_id + " pass gsr'          class='col51'>" + summary.sr.pass          + "</td>";
    html += "      <td headers='" + row_id + " fail gsr'          class='col52'>"  + summary.sr.fail          + "</td>";
    html += "      <td headers='" + row_id + " question gsr'      class='col53'>"  + summary.sr.question      + "</td>";
    html += "      <td headers='" + row_id + " untested gsr'      class='col54'>"  + summary.sr.untested      + "</td>";
    html += "    </tr>";    
  } // endfor
  

    html += "    <tr>";    
    html += "      <th id='gtotals' colspan='2'>Totals</th>"; 
    html += "      <td headers='gtotals gnum'>"; 
    html += "        " + totals.num;
    html += "      </td>";
    html += "      <td headers='gtotals pass gwin7'        class='col11'>" + totals.win7.pass        + "</td>";
    html += "      <td headers='gtotals fail gwin7'        class='col12'>" + totals.win7.fail        + "</td>";
    html += "      <td headers='gtotals question gwin7'    class='col13'>" + totals.win7.question    + "</td>";
    html += "      <td headers='gtotals untested gwin7'    class='col14'>" + totals.win7.untested    + "</td>";
    html += "      <td headers='gtotals pass gosx106'      class='col21'>" + totals.osx106.pass      + "</td>";
    html += "      <td headers='gtotals fail gosx106'      class='col22'>" + totals.osx106.fail      + "</td>";
    html += "      <td headers='gtotals question gosx106'  class='col23'>" + totals.osx106.question  + "</td>";
    html += "      <td headers='gtotals fail gosx106'      class='col24'>" + totals.osx106.untested  + "</td>";
    html += "      <td headers='gtotals pass gunix'        class='col31'>" + totals.unix.pass        + "</td>";
    html += "      <td headers='gtotals pass gunix'        class='col32'>" + totals.unix.fail        + "</td>";
    html += "      <td headers='gtotals pass gunix'        class='col33'>" + totals.unix.question    + "</td>";
    html += "      <td headers='gtotals pass gunix'        class='col34'>" + totals.unix.untested    + "</td>";
    html += "      <td headers='gtotals pass gwin7uia'     class='col41'>" + totals.win7uia.pass     + "</td>";
    html += "      <td headers='gtotals fail gwin7uia'     class='col42'>" + totals.win7uia.fail     + "</td>";
    html += "      <td headers='gtotals question gwin7uia' class='col43'>" + totals.win7uia.question + "</td>";
    html += "      <td headers='gtotals untested gwin7uia' class='col44'>" + totals.win7uia.untested + "</td>";
    html += "      <td headers='gtotals pass gsr'          class='col51'>" + totals.sr.pass          + "</td>";
    html += "      <td headers='gtotals fail gsr'          class='col52'>"  + totals.sr.fail          + "</td>";
    html += "      <td headers='gtotals question gsr'      class='col53'>"  + totals.sr.question      + "</td>";
    html += "      <td headers='gtotals untested gsr'      class='col54'>"  + totals.sr.untested      + "</td>";
    html += "    </tr>";    


  html += "  </tbody>";
  
  html += " </table>";
  
  $('div#group').append(html);

} // end ready function
); // end ready event


$(document).ready( function() {

  var html = "";

  html += "<table class='testresults' summary='All test results'>";
  html += "  <caption>All Test Results</caption>";
  html += "  <thead>";
  html += "    <tr>";
  html += "      <th id='test' rowspan='2'>Test<br/>ID</th>";
  html += "      <th id='desc' rowspan='2'>Description</th>";
  html += "      <th id='stat' rowspan='2'>States</th>";
  html += "      <th id='prop' rowspan='2'>Properties</th>";
  html += "      <th id='msaa' colspan='2' class='col1'>Windows 7 <abbr title='Microsoft Active Accessibility'>MSAA</abbr> &amp; <abbr title='IAccessible2'>IA2</abbr></th>";
  html += "      <th id='osx'  colspan='2' class='col2'>OS X 10.6</th>";
  html += "      <th id='unix' colspan='2' class='col3'>Unix/Linux</th>";
  html += "      <th id='uia'  colspan='2' class='col4'>Windows 7 UI Automation</th>";
  html += "      <th id='sr'   colspan='2' class='col5'>Screen Reader</th>";
  html += "    </tr>";
  html += "    <tr>";
  html += "      <th id='mr'  class='col11'>Mapping Results</th>";
  html += "      <th id='anr' class='col12'>Accessible Name Results</th>";
  html += "      <th class='col21'>Mapping Results</th>";
  html += "      <th class='col22'>Accessible Name Results</th>";
  html += "      <th class='col31'>Mapping Results</th>";
  html += "      <th class='col32'>Accessible Name Results</th>";
  html += "      <th class='col41'>Mapping Results</th>";
  html += "      <th class='col42'>Accessible Name Results</th>";
  html += "      <th class='col51'>Mapping Results</th>";
  html += "      <th class='col52'>Accessible Name Results</th>";
  html += "    </tr>";
  html += "  </thead>";

  for(var i = 0; i < aria_test_results.results.length; i++ ) {
  
    var id = aria_test_results.results[i].id.split("_");    
    var group = parseInt(id[1],10);
    var num   = parseInt(id[2],10);
    var test_id = "test" + group + "_" + num; 

    html += "  <tbody>";
    html += "    <tr>";
    html += "      <th id='" + test_id + "'>" + group + "." + num + "</th>";
    html += "      <td headers='" + test_id + " desc' class='desc'>";
    html += "        <a href='" + aria_test_results.results[i].path + aria_test_results.results[i].file + "'>"+ aria_test_results.results[i].title + "</a>";
    html += "      </td>";
    // States
    html += "      <td headers='" + test_id + " stat'>";
    html += "        <ul>";
    var states = aria_test_results.results[i].states;
    for(var j=0; j < states.length; j++) {
      html += "        <li>" + states[j] +  "</li>";  
    } // endfor    
    html += "         </ul>";
    html += "       </td>";

    // Properties
    html += "      <td headers='" + test_id + " prop'>";
    html += "        <ul>";
    var properties = aria_test_results.results[i].properties;
    for(var j=0; j < properties.length; j++) {
      html += "        <li>" + properties[j] +  "</li>";  
    } // endfor    
    html += "         </ul>";
    html += "       </td>";
    
    var win7 = aria_test_results.results[i].results.win7;
    if( win7 ) {
      html += "      <td headers='" + test_id + " msaa mr'  class='col11'>";
      html += "        <ul>";
      for(var j=0; j < win7.length; j++) {
        html += "        <li>" + ARIA_TEST_CONSTANTS.USER_AGENT[win7[j].name] + " " + ARIA_TEST_CONSTANTS.RESULT[win7[j].result] + "</li>";  
      } // endfor
      html += "        </ul>";    
      html += "      </td>";
    

      html += "      <td headers='" + test_id + " msaa anr' class='col12'>";
      html += "        <ul>";
    
      for(var j=0; j < win7.length; j++) {
        html += "        <li>" + ARIA_TEST_CONSTANTS.USER_AGENT[win7[j].name] + " " + ARIA_TEST_CONSTANTS.RESULT[win7[j].accessible_name] + "</li>";  
      } // endfor
      html += "        </ul>";    
      html += "      </td>";
    } else {
      html += "      <td headers='" + test_id + " osx mr' class='col11'><span class='nr'>no results</span></td>";
      html += "      <td headers='" + test_id + " osx anr' class='col12'><span class='nr'>no results</span></td>";    
    } // endif  
    
    

    var osx = aria_test_results.results[i].results.osx106;
    if( osx ) {
      html += "      <td headers='" + test_id + " osx mr' class='col21'>";
      html += "        <ul>";    
    
      for(var j=0; j < osx.length; j++) {
        html += "        <li>" + ARIA_TEST_CONSTANTS.USER_AGENT[osx[j].name] + " " + ARIA_TEST_CONSTANTS.RESULT[osx[j].result] + "</li>";  
      } // endfor
    
      html += "        </ul>";    
      html += "      </td>";
 
      html += "      <td headers='" + test_id + " osx anr' class='col22'>"
      html += "        <ul>";    
    
      for(var j=0; j < osx.length; j++) {
        html += "        <li>" + ARIA_TEST_CONSTANTS.USER_AGENT[osx[j].name] + " " + ARIA_TEST_CONSTANTS.RESULT[osx[j].accessible_name] + "</li>";  
      } // endfor
    
      html += "        </ul>";    
      html += "      </td>";
    } else {
      html += "      <td headers='" + test_id + " osx mr' class='col21'><span class='nr'>no results</span></td>";
      html += "      <td headers='" + test_id + " osx anr' class='col22'><span class='nr'>no results</span></td>";    
    } // endif  
    
    
    var unix = aria_test_results.results[i].results.unix;
    if( unix ) {
      html += "      <td headers='" + test_id + " unix mr' class='col31'>";
      html += "        <ul>";    
    
      for(var j=0; j < unix.length; j++) {
        html += "        <li>" + ARIA_TEST_CONSTANTS.USER_AGENT[unix[j].name] + " " + ARIA_TEST_CONSTANTS.RESULT[unix[j].result] + "</li>";  
      } // endfor
    
      html += "        </ul>";    
      html += "      </td>";
 
      html += "      <td headers='" + test_id + " unix anr' class='col32'>"
      html += "        <ul>";    
    
      for(var j=0; j < unix.length; j++) {
        html += "        <li>" + ARIA_TEST_CONSTANTS.USER_AGENT[unix[j].name] + " " + ARIA_TEST_CONSTANTS.RESULT[unix[j].accessible_name] + "</li>";  
      } // endfor
    
      html += "        </ul>";    
      html += "      </td>";
    } else {
      html += "      <td headers='" + test_id + " unix mr' class='col31'><span class='nr'>no results</span></td>";
      html += "      <td headers='" + test_id + " unix anr' class='col32'><span class='nr'>no results</span></td>";    
    } // endif  
    
    var win7uia = aria_test_results.results[i].results.win7uia;
    if( win7uia ) {
      html += "      <td headers='" + test_id + " uia mr' class='col41'>";
      html += "        <ul>";    

      for(var j=0; j < win7uia.length; j++) {
        html += "        <li>" + ARIA_TEST_CONSTANTS.USER_AGENT[win7uia[j].name] + " " + ARIA_TEST_CONSTANTS.RESULT[win7uia[j].result] + "</li>";  
      } // endfor
    
      html += "        </ul>";    
      html += "      </td>";
 
      html += "      <td headers='" + test_id + " uia anr' class='col42'>"
      html += "        <ul>";    
    
      for(var j=0; j < win7uia.length; j++) {
        html += "        <li>" + ARIA_TEST_CONSTANTS.USER_AGENT[win7uia[j].name] + " " + ARIA_TEST_CONSTANTS.RESULT[win7uia[j].accessible_name] + "</li>";  
      } // endfor
    
      html += "        </ul>";    
      html += "      </td>";
    } else {
      html += "      <td headers='" + test_id + " uia mr' class='col41'>no results</td>";
      html += "      <td headers='" + test_id + " uia mr' class='col42'>no results</td>";    
    } // endif  
    
    
    var sr = aria_test_results.results[i].results.sr;
    if( sr ) {

      html += "      <td headers='" + test_id + " sr mr' class='col51'>";
      html += "        <ul>";    

      for(var j=0; j < sr.length; j++) {
        html += "        <li>" + ARIA_TEST_CONSTANTS.USER_AGENT[sr[j].name] + " " + ARIA_TEST_CONSTANTS.RESULT[sr[j].result] + "</li>";  
      } // endfor
    
      html += "        </ul>";    
      html += "      </td>";
 
      html += "      <td headers='" + test_id + " sr anr' class='col52'>"
      html += "        <ul>";    
    
      for(var j=0; j < sr.length; j++) {
        html += "        <li>" + ARIA_TEST_CONSTANTS.USER_AGENT[sr[j].name] + " " + ARIA_TEST_CONSTANTS.RESULT[sr[j].accessible_name] + "</li>";  
      } // endfor
    
      html += "        </ul>";    
      html += "      </td>";   
    } else {
      html += "      <td headers='" + test_id + " sr mr'  class='col51'><span class='nr'>no results</span></td>";
      html += "      <td headers='" + test_id + " sr anr' class='col52'><span class='nr'>no results</span></td>";    
    } // endif  



    html += "    </tr>";
    
    html += "  </tbody>";
  }  // end for 

  html += "</table>";

  $('div#all').append(html);

  }  // end ready function
) // end ready event

